﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Web;
using System.Data;
using System.IO;

namespace JigsawService.Utility
{
    public class XMLUtility
    {
        public XMLUtility()
        {

        }

        /// <summary>
        /// Deprecated, params no longer passed as XML
        /// </summary>
        /// <param name="reqMsg"></param>
        /// <returns></returns>
        public string EncodeParamValues(string reqMsg)
        {
            string encodedMsg = reqMsg;

            // method params (values) may contain special characters
            int paramstart = reqMsg.IndexOf("<MethodParams>");
            while (reqMsg.IndexOf("<Value>", paramstart) > 0)
            {
                int paramvalstart = reqMsg.IndexOf("<Value>", paramstart) + 7;
                paramstart = paramvalstart;
                int paramvalend = reqMsg.IndexOf("</Value>", paramvalstart);
                string rawparamval = reqMsg.Substring(paramvalstart, paramvalend - paramvalstart);
                string encodedparamval = XMLEncode(rawparamval);
                string tmpstr3 = reqMsg.Remove(paramvalstart, paramvalend - paramvalstart);
                reqMsg = tmpstr3.Insert(paramvalstart, encodedparamval);
                paramstart = paramvalend + 8;
            }

            return encodedMsg;
        }

        /// <summary>
        /// < =	&lt;
        /// > =	&gt;
        /// " =	&quot;
        /// ' =	&apos;
        /// & = &amp;
        /// </summary>
        /// <param name="inputStr"></param>
        /// <returns></returns>
        public string XMLEncode(string inputStr)
        {
            string encodedStr = inputStr.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");
            return encodedStr;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="inputStr"></param>
        /// <returns></returns>
        public string XMLDecode(string inputStr)
        {
            string decodedStr = inputStr.Replace("&amp;", "&").Replace("&lt;", "<").Replace("&gt;", ">").Replace("&quot;", "\"").Replace("&apos;", "'");
            return decodedStr;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dtable"></param>
        /// <param name="schemaFlag"></param>
        /// <returns></returns>
        public string TableToXml(DataTable dtable, bool schemaFlag)
        {
            StringWriter sw = new StringWriter();

            dtable.TableName = "Record";
            if (schemaFlag)
            {
                // NOTE: WriteSchema returns a serialized DataSet
                dtable.WriteXml(sw, XmlWriteMode.WriteSchema);
            }
            else
            {
                // NOTE: IgnoreSchema returns an XML fragment
                dtable.WriteXml(sw, XmlWriteMode.IgnoreSchema);
            }

            return sw.ToString();
        }

    }
}